package fr.miage.orleans.aar.administration;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.EJB;
import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import fr.miage.orleans.aar.ejb.FacadeGettersBean;
import fr.miage.orleans.aar.ejb.FacadeGettersVoBean;
import fr.miage.orleans.aar.ejb.administration.FacadeAdministrationBean;
import fr.miage.orleans.aar.modele.Administrateur;
import fr.miage.orleans.aar.modele.Arbitre;
import fr.miage.orleans.aar.modele.ButMatch;
import fr.miage.orleans.aar.modele.ButTirAuBut;
import fr.miage.orleans.aar.modele.Equipe;
import fr.miage.orleans.aar.modele.EtatMatchTour;
import fr.miage.orleans.aar.modele.MatchTour;
import fr.miage.orleans.aar.modele.Tournoi;
import fr.miage.orleans.aar.modele.valueobject.ValueObjectButMatch;
import fr.miage.orleans.aar.modele.valueobject.ValueObjectEquipeDetail;
import fr.miage.orleans.aar.modele.valueobject.ValueObjectEquipeLight;
import fr.miage.orleans.aar.modele.valueobject.ValueObjectEquipeMinimal;
import fr.miage.orleans.aar.modele.valueobject.ValueObjectJoueur;
import fr.miage.orleans.aar.modele.valueobject.ValueObjectMatch;
import fr.miage.orleans.aar.modele.valueobject.ValueObjectTournoi;
import fr.miage.orleans.aar.utils.NotifUtils;

/**
 *
 * @author Steve Cancès <steve.cances@gmail.com>
 */
@WebServlet(name = "AdministrationServlet", urlPatterns = {"/administration"})
public class AdministrationServlet extends HttpServlet {

	/**
	 * Les strings
	 */
	private static final String STRING_ACTION = "action";
	/**
	 * Les actions
	 */
	private static final String ACTION_LOGIN = "login";
	private static final String ACTION_LOGOUT = "logout";
	private static final String ACTION_CREER_TOURNOI = "creerTournoi";
	private static final String ACTION_CREER_ARBITRE = "creerArbitre";
	private static final String ACTION_VALIDER_TOURNOI = "validerTournoi";
	private static final String ACTION_DETAIL_TOURNOI = "detailTournoi";
	private static final String ACTION_DESINSCRIRE_EQUIPE = "desinscrireEquipe";
	private static final String ACTION_AFFECTER_ARBITRE = "affecterArbitre";
	private static final String ACTION_COMMENCER_MATCH = "commencerMatch";
	private static final String ACTION_DETAIL_MATCH = "detailMatch";
	private static final String ACTION_AJOUTER_BUT = "ajouterBut";
	private static final String ACTION_TERMINER_PERIODE = "terminerPeriode";
	private static final String ACTION_VALIDE_SERIE_TIR_BUT = "validerSerieTirsAuBut";
	//private static final String ACTION_VALIDE_SERIE_TIR_AU_BUT_MORT_SUBITE = "validerSerieTirsAuButMS";

	/**
	 * Les JSPs
	 */
	private static final String JSP_ADMINISTRATION_LOGIN = "/WEB-INF/administration_login.jsp";
	private static final String JSP_ADMINISTRATION = "/WEB-INF/administration.jsp";
	private static final String JSP_ADMINISTRATION_DETAIL_TOURNOI = "/WEB-INF/administration_detail_tournoi.jsp";
	private static final String JSP_ADMINISTRATION_DETAIL_MATCH = "/WEB-INF/administration_detail_match.jsp";
	//private static final String JSP_ADMINISTRATION_DETAIL_MATCH_TIRS_AU_BUT = "/WEB-INF/administration_detail_match_tirs_au_but.jsp";

	/**
	 * Les attributs de session
	 */
	public static final String SA_ADMINISTRATEUR = "administrateur";
	//public static final String SA_ERROR = "error";
	public static final String SA_TOURNOIS = "tournois";
	public static final String SA_TOURNOI = "tournoi";
	public static final String SA_EQUIPES = "equipes";
	public static final String SA_ARBITRES = "arbitres";
	public static final String SA_MATCH = "match";
	public static final String SA_EQUIPE_A = "equipeA";
	public static final String SA_EQUIPE_B = "equipeB";
	public static final String SA_INTEGER_FROM = "integerFrom";
	public static final String SA_INTEGER_TO = "integerTo";
	public static final String SA_NBSERIE = "numSerie";
	//public static final String SA_SUCCESS  ="notif";
	//public static final String SA_LISTE_TIR__BUT_A ="listeTirButA";
	//public static final String SA_LISTE_TIR__BUT_B ="listeTirButB";

	/**
	 * Les parametres
	 */
	private static final String PARAM_LOGIN = "login";
	private static final String PARAM_PASSWORD = "password";
	private static final String PARAM_TOURNOI_ID = "tournoiId";
	private static final String PARAM_EQUIPE_ID = "equipeId";
	private static final String PARAM_MATCH_ID = "matchId";
	private static final String PARAM_PERSONNE_ID = "personneId";
	private static final String PARAM_NOM_TOURNOI = "nomTournoi";
	private static final String PARAM_NOM_ARBITRE = "nomArbitre";
	private static final String PARAM_PRENOM_ARBITRE = "prenomArbitre";
	private static final String PARAM_INPUT_MINUTE = "inputMinute";
	private static final String PARAM_REUSSITE = "checkBut";
	//private static final String PARAM_RANG_TIR_BUT = "numSerie";


	@EJB
	private FacadeGettersBean facadeGetters;

	@EJB
	private FacadeGettersVoBean facadeGettersVo;

	@EJB
	private FacadeAdministrationBean facadeAdministration;

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		HttpSession session = request.getSession(true);
		String action = request.getParameter(STRING_ACTION);
		Administrateur administrateur = (Administrateur) session.getAttribute(SA_ADMINISTRATEUR);
		if (administrateur == null) {
			if (action == null || action.isEmpty()) {
				this.processIndexLogin(request, response);
			} else {
				switch (action) {
				case ACTION_LOGIN: {
					this.processLogin(request, response);
					break;
				}
				default: {
					this.processIndexLogin(request, response);
				}
				}
			}
		} else {
			if (action == null || action.isEmpty()) {
				this.processIndex(request, response);
			} else {
				switch (action) {
				case ACTION_LOGOUT: {
					this.processLogout(request, response);
					break;
				}
				case ACTION_CREER_TOURNOI: {
					this.processCreerTournoi(request, response);
					break;
				}
				case ACTION_CREER_ARBITRE: {
					this.processCreerArbitre(request, response);
					break;
				}
				case ACTION_VALIDER_TOURNOI: {
					this.processValiderTournoi(request, response);
					break;
				}
				case ACTION_DETAIL_TOURNOI: {
					this.processDetailTournoi(request, response);
					break;
				}
				case ACTION_DESINSCRIRE_EQUIPE: {
					this.processDesinscrireEquipe(request, response);
					break;
				}
				case ACTION_AFFECTER_ARBITRE: {
					this.processAffecterArbitre(request, response);
					break;
				}
				case ACTION_COMMENCER_MATCH: {
					this.processCommencerMatch(request, response);
					break;
				}
				case ACTION_DETAIL_MATCH: {
					this.processDetailMatch(request, response);
					break;
				}
				case ACTION_AJOUTER_BUT: {
					this.processAjouterBut(request, response);
					break;
				}
				case ACTION_TERMINER_PERIODE: {
					this.processTerminerPeriode(request, response);
					break;
				}
				case ACTION_VALIDE_SERIE_TIR_BUT:{
					this.procesAjouterButTirAuBut(request, response);
					break;
				}
				default: {
					this.processIndex(request, response);
				}
				}
			}
		}
	}

	protected void processIndexLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher(JSP_ADMINISTRATION_LOGIN).forward(request, response);
	}

	protected void processLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		String login = request.getParameter(PARAM_LOGIN);
		String password = request.getParameter(PARAM_PASSWORD);
		if (login != null && password != null) {
			Administrateur administrateur = this.facadeAdministration.logIn(login, password);
			if (administrateur != null) {
				session.setAttribute(SA_ADMINISTRATEUR, administrateur);
				this.processIndex(request, response);
			} else {
				session.setAttribute(NotifUtils.SA_ERROR, "Login et/ou Mot de passe incorrects");
				this.processIndexLogin(request, response);
			}
		} else {
			this.processIndexLogin(request, response);
		}
	}

	protected void processLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		session.removeAttribute(SA_ADMINISTRATEUR);
		this.processIndexLogin(request, response);
	}

	protected void processIndex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		Collection<Tournoi> tournois = this.facadeGetters.getAllTournois();
		session.setAttribute(SA_TOURNOIS, tournois);
		Collection<ValueObjectEquipeDetail> equipes = this.facadeGettersVo.getVoAllEquipes();
		session.setAttribute(SA_EQUIPES, equipes);
		Collection<Arbitre> arbitres = this.facadeAdministration.getAllArbitres();
		session.setAttribute(SA_ARBITRES, arbitres);
		request.getRequestDispatcher(JSP_ADMINISTRATION).forward(request, response);
	}

	protected void processCreerTournoi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nomTournoi = request.getParameter(PARAM_NOM_TOURNOI);
		if (nomTournoi != null && !nomTournoi.isEmpty()) {
			this.facadeAdministration.creerNouveauTournoi(nomTournoi);
			request.getSession().setAttribute(NotifUtils.SA_SUCCESS, "Le tournoi " + nomTournoi + " a été crée");
		}
		else
		{
			request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le tournoi n'a pas été crée");
		}
		
		this.processIndex(request, response);
	}

	protected void processCreerArbitre(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nomArbitre = request.getParameter(PARAM_NOM_ARBITRE);
		String prenomArbitre = request.getParameter(PARAM_PRENOM_ARBITRE);
		if (nomArbitre != null && !nomArbitre.isEmpty() && prenomArbitre != null && !prenomArbitre.isEmpty()) {
			this.facadeAdministration.creerArbitre(nomArbitre, prenomArbitre);
			request.getSession().setAttribute(NotifUtils.SA_ERROR, "L'arbitre " + nomArbitre + " " + prenomArbitre + " a été crée.");
		}
		else
		{
			request.getSession().setAttribute(NotifUtils.SA_ERROR, "L'arbitre n'a pas été crée");
		}
		this.processIndex(request, response);
	}

	protected void processValiderTournoi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tournoiId = request.getParameter(PARAM_TOURNOI_ID);
		if (!tournoiId.isEmpty()) {
			try {
				int id = Integer.valueOf(tournoiId);
				boolean ok = this.facadeAdministration.validerPreparationTournoi(id);
				if (ok) {
					request.getSession().setAttribute(NotifUtils.SA_SUCCESS, "Le tournoi a démarré");
					this.processDetailTournoi(request, response);
				} else {
					request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le tournoi n'a pas pu démarrer");
					this.processIndex(request, response);
				}
			} catch (NumberFormatException ex) {
				request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le tournoi n'a pas pu démarrer");
				this.processIndex(request, response);
			}
		} else {
			request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le tournoi n'a pas pu démarrer");
			this.processIndex(request, response);
		}
	}

	protected void processDetailTournoi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tournoiId = request.getParameter(PARAM_TOURNOI_ID);

		if (tournoiId == null || tournoiId.isEmpty()) {
			Integer tournoiIdByAttr = (Integer) request.getAttribute(PARAM_TOURNOI_ID);
			tournoiId = tournoiIdByAttr.toString();
		}

		if (tournoiId != null && !tournoiId.isEmpty()) {
			try {
				int id = Integer.valueOf(tournoiId);
				ValueObjectTournoi tournoi = this.facadeGettersVo.getVoTournoi(id);
				HttpSession session = request.getSession(true);
				session.setAttribute(SA_TOURNOI, tournoi);
				Collection<Arbitre> arbitres = this.facadeAdministration.getAllArbitres();
				session.setAttribute(SA_ARBITRES, arbitres);
				request.getRequestDispatcher(JSP_ADMINISTRATION_DETAIL_TOURNOI).forward(request, response);

			} catch (NumberFormatException ex) {
				this.processIndex(request, response);
			}
		} else {
			this.processIndex(request, response);
		}
	}

	protected void processDesinscrireEquipe(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String paramTournoiId = request.getParameter(PARAM_TOURNOI_ID);
		String paramEquipeId = request.getParameter(PARAM_EQUIPE_ID);
		if (!paramTournoiId.isEmpty() && !paramEquipeId.isEmpty()) {
			try {
				int tournoiId = Integer.valueOf(paramTournoiId);
				int equipeId = Integer.valueOf(paramEquipeId);
				this.facadeAdministration.desinscrireEquipeTournoi(tournoiId, equipeId);
				this.processDetailTournoi(request, response);

			} catch (NumberFormatException ex) {
				this.processDetailTournoi(request, response);
			}
		} else {
			this.processIndex(request, response);
		}
	}

	protected void processAffecterArbitre(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		String paramMatchId = request.getParameter(PARAM_MATCH_ID);
		String paramPersonneId = request.getParameter(PARAM_PERSONNE_ID);
		RetourJson retourJson;
		if (!paramMatchId.isEmpty() && !paramPersonneId.isEmpty()) {
			try {
				int arbitreId = Integer.valueOf(paramPersonneId);
				int matchId = Integer.valueOf(paramMatchId);
				MatchTour matchTour = this.facadeAdministration.affecterArbitreMatch(matchId, arbitreId);
				Arbitre arbitre = matchTour.getArbitre();

				Map<String, String> values = new HashMap<>();
				values.put("nomPrenom", arbitre.getPrenom() + " " + arbitre.getNom());
				retourJson = new RetourJson("OK", values);

			} catch (NumberFormatException ex) {
				retourJson = new RetourJson("KO");
			}
		} else {
			retourJson = new RetourJson("KO");
		}
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		out.print(gson.toJson(retourJson));
		out.flush();
	}

	private class RetourJson {

		private String resultCode;
		private Map<String, String> values;

		public RetourJson(String resultCode) {
			this.resultCode = resultCode;
		}

		public RetourJson(String resultCode, Map<String, String> values) {
			this(resultCode);
			this.values = values;
		}

		public String getResultCode() {
			return resultCode;
		}

		public Map<String, String> getValues() {
			return values;
		}

	}

	protected void processCommencerMatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String paramMatchId = request.getParameter(PARAM_MATCH_ID);
		if (!paramMatchId.isEmpty()) {
			try {
				int matchId = Integer.valueOf(paramMatchId);
				boolean ok = this.facadeAdministration.commencerPremiereMiTemps(matchId);
				if (ok) {
					request.getSession().setAttribute(NotifUtils.SA_SUCCESS, "Le match a démarré");
					this.processDetailMatch(request, response);
					
				} else {
					request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le match n'a pas pu démarrer");
					this.processIndex(request, response);
				}
			} catch (IOException | NumberFormatException | ServletException e) {
				request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le match n'a pas pu démarrer");
				this.processIndex(request, response);
			}
		} else {
			request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le match n'a pas pu démarrer");
			this.processIndex(request, response);
		}
	}

	protected void processDetailMatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String paramMatchId = request.getParameter(PARAM_MATCH_ID);
		if (!paramMatchId.isEmpty()) {
			try {
				int matchId = Integer.valueOf(paramMatchId);
				System.out.println("matchId : " + matchId);
				ValueObjectMatch match = this.facadeGettersVo.getVOMatch(matchId);
				Collections.sort(match.getListButs(), new Comparator<ValueObjectButMatch>() {
					@Override
					public int compare(ValueObjectButMatch but1, ValueObjectButMatch but2) {
						return new Integer(but1.getMinuteBut()).compareTo(but2.getMinuteBut());
					}
				});

				Comparator<ValueObjectJoueur> comparatorJoueurs = new Comparator<ValueObjectJoueur>() {

					@Override
					public int compare(ValueObjectJoueur j1, ValueObjectJoueur j2) {
						return j1.getPosteJoueur().getNomPoste().compareTo(j2.getPosteJoueur().getNomPoste());
					}
				};

				ValueObjectEquipeLight equipeA = this.facadeGettersVo.getVoEquipeLight(match.getEquipeA().getId());
				Collections.sort(equipeA.getListeJoueur(), comparatorJoueurs);
				ValueObjectEquipeLight equipeB = this.facadeGettersVo.getVoEquipeLight(match.getEquipeB().getId());
				Collections.sort(equipeB.getListeJoueur(), comparatorJoueurs);

				Integer integerFrom= null;
				Integer integerTo= null;
				switch (match.getEtatMatch()) {
				case PREMIERE_MI_TEMPS: {
					integerFrom = 1;
					integerTo = 45;
					break;
				}
				case DEUXIEME_MI_TEMPS: {
					integerFrom = 46;
					integerTo = 90;
					break;
				}
				case PROLONGATION_PREMIERE_MI_TEMPS: {
					integerFrom = 91;
					integerTo = 105;
					break;
				}
				case PROLONGATION_DEUXIEME_MI_TEMPS: {
					integerFrom = 106;
					integerTo = 120;
					break;
				}
				default: {
					integerFrom = 0;
					integerTo = 0;
				}
				}


				HttpSession session = request.getSession(true);
				match = this.facadeGettersVo.getVOMatch(matchId);
				System.out.println(match);
				session.setAttribute(SA_MATCH, match);
				session.setAttribute(SA_EQUIPE_A, equipeA);
				session.setAttribute(SA_EQUIPE_B, equipeB);
				session.setAttribute(SA_INTEGER_TO, integerTo);
				session.setAttribute(SA_INTEGER_FROM, integerFrom);

				if (match.getEtatMatch()==EtatMatchTour.TIR_AU_BUT || match.getEtatMatch() == EtatMatchTour.MORT_SUBITE){
					session.setAttribute(SA_NBSERIE, match.getListTOB().size()/2 +1 );
					request.getRequestDispatcher(JSP_ADMINISTRATION_DETAIL_MATCH).forward(request, response);

				}
				else{
					request.getRequestDispatcher(JSP_ADMINISTRATION_DETAIL_MATCH).forward(request, response);
				}


			} catch (IOException | NumberFormatException | ServletException | NullPointerException e) {
				e.printStackTrace();
				this.processIndex(request, response);
			}
		} else {
			this.processIndex(request, response);
		}
	}

	protected void processAjouterBut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String paramMatchId = request.getParameter(PARAM_MATCH_ID);
		String paramPersonneId = request.getParameter(PARAM_PERSONNE_ID);
		String paramEquipeId = request.getParameter(PARAM_EQUIPE_ID);
		String paramInputMinute = request.getParameter(PARAM_INPUT_MINUTE);
		if (!paramMatchId.isEmpty() && !paramPersonneId.isEmpty() && !paramEquipeId.isEmpty() && !paramInputMinute.isEmpty()) {
			try {
				int matchId = Integer.valueOf(paramMatchId);
				int personneId = Integer.valueOf(paramPersonneId);
				int equipeId = Integer.valueOf(paramEquipeId);
				int minute = Integer.valueOf(paramInputMinute);

				ButMatch but = this.facadeAdministration.addButMatch(matchId, equipeId, personneId, minute);
				request.getSession().setAttribute(NotifUtils.SA_SUCCESS, "Un but a été ajouté à l'équipe " + but.getEquipe().getNom());
				this.processDetailMatch(request, response);

			} catch (NumberFormatException exception) {
				log(exception.getMessage(), exception);
				request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le but n'a pas pu être ajouté");
				this.processIndex(request, response);
			}
		} else {
			request.getSession().setAttribute(NotifUtils.SA_ERROR, "Le but n'a pas pu être ajouté");
			this.processIndex(request, response);
		}
	}

	protected void processTerminerPeriode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String paramMatchId = request.getParameter(PARAM_MATCH_ID);
		if (!paramMatchId.isEmpty()) {
			try {
				int matchId = Integer.valueOf(paramMatchId);

				try {
					this.facadeAdministration.terminerPeriode(matchId);
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println("----------------------------");
				}

				MatchTour matchTour = this.facadeGetters.getMatchTourById(matchId);
				if (matchTour != null) {
					EtatMatchTour emt = matchTour.getEtatMatch();

					if (emt == EtatMatchTour.TERMINE_A_GAGNE || emt == EtatMatchTour.TERMINE_B_GAGNE || emt == EtatMatchTour.NON_COMMENCE) {
						request.setAttribute(PARAM_TOURNOI_ID, Integer.valueOf(matchTour.getTournoi().getTournoiId()));
						Equipe equipeGagnante;
						if(emt == EtatMatchTour.TERMINE_A_GAGNE)
						{
							equipeGagnante = matchTour.getEquipeA();
						}
						else
						{
							equipeGagnante = matchTour.getEquipeB();
						}
						request.getSession().setAttribute(NotifUtils.SA_SUCCESS, "Le match est terminé - " + equipeGagnante.getNom() + " a gagné");
						this.processDetailTournoi(request, response);
					} else {
						request.getSession().setAttribute(NotifUtils.SA_SUCCESS, "Le match est passé en " + emt.toString());	
						this.processDetailMatch(request, response);
					}

				}

				else {
					request.getSession().setAttribute(NotifUtils.SA_ERROR, "Une erreur est survenue");	
					this.processIndex(request, response);
				}
			} catch (IOException | NumberFormatException | ServletException exception) {
				log(exception.getMessage(), exception);
				request.getSession().setAttribute(NotifUtils.SA_ERROR, "Une erreur est survenue");	
				this.processIndex(request, response);
			}
		} else {
			request.getSession().setAttribute(NotifUtils.SA_ERROR, "Une erreur est survenue");	
			this.processIndex(request, response);
		}
	}
	
	private void procesAjouterButTirAuBut(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String paramMatchId = request.getParameter(PARAM_MATCH_ID);
		int paramPersonneId = Integer.parseInt(request.getParameter(PARAM_PERSONNE_ID));
		int paramEquipeId = Integer.parseInt(request.getParameter(PARAM_EQUIPE_ID));
		boolean paramReussite = request.getParameter(PARAM_REUSSITE) != null;
		int paramMatchIdint = Integer.valueOf(paramMatchId);				
		ButTirAuBut btob = facadeAdministration.addTirAuBut(paramMatchIdint, paramEquipeId, paramPersonneId, paramReussite);
		ValueObjectMatch vom = facadeGettersVo.getVOMatch(paramMatchIdint);
		if(vom.getEtatMatch() == EtatMatchTour.TERMINE_A_GAGNE || vom.getEtatMatch() == EtatMatchTour.TERMINE_B_GAGNE)
		{
			ValueObjectEquipeMinimal equipeGagnante;
			if(vom.getEtatMatch() == EtatMatchTour.TERMINE_A_GAGNE)
			{
				equipeGagnante = vom.getEquipeA();
			}
			else
			{
				equipeGagnante = vom.getEquipeB();
			}
			request.getSession().setAttribute(NotifUtils.SA_SUCCESS, "Le match est terminé - " + equipeGagnante.getNom());
			request.setAttribute(PARAM_TOURNOI_ID, Integer.valueOf(vom.getTournoiId()));
			this.processDetailTournoi(request, response);
		}
		else
		{
			request.getSession().setAttribute(NotifUtils.SA_SUCCESS, "Un tir au but a été ajouté à " + btob.getEquipe().getNom());
			this.processDetailMatch(request, response);
		}
	}



	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if§ an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 *
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}
